package com.qfedu.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.log.dto.LogDto;
import com.qfedu.log.mapper.LogMapper;
import com.qfedu.log.pojo.Log;
import com.qfedu.log.service.LogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {

    @Transactional(readOnly = true)
    @Override
    public Page<Log> getLogs(LogDto logDto) {
        LambdaQueryWrapper<Log> wrapper = new LambdaQueryWrapper<>();
        String username = logDto.getUsername();
        wrapper.eq(StringUtils.hasLength(username), Log::getUser, username);
        Integer error = logDto.getError();
        wrapper.eq(error != null, Log::getError, error);
        String dateFrom = logDto.getDateFrom();
        wrapper.ge(StringUtils.hasLength(dateFrom), Log::getOperationTime, dateFrom);
        String dateTo = logDto.getDateTo();
        wrapper.le(StringUtils.hasLength(dateTo), Log::getOperationTime, dateTo);

        Page<Log> page = new Page<>(logDto.getCurrentPage(), logDto.getPageSize());
        return page(page, wrapper);
    }
}
